<template>
  <el-container>
    <el-header>
      <el-row class="header-row">
        名称:<el-input v-model="conditions[0].value" style="width:150px;" placeholder="请输入内容" clearable />
        <el-button class="table-header-btn" type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </el-row>
    </el-header>
    <el-main>
      <el-col :span="1"><div class="grid-content bg-purple-light">
        <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addClick">新增</el-button>
      </div></el-col>
      <template>
        <el-table :data="jsonDictionaryList" stripe border style="width: 100%">
          <el-table-column prop="name" label="名称" width="180" />
          <el-table-column prop="code" label="编号" />
          <el-table-column prop="create_time" label="创建时间" width="180" />
          <el-table-column label="是否可删除" width="180">
            <template slot-scope="scope">
              {{ key_logic[scope.row.del_flag] }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
              <el-button type="text" size="small" @click="editClick(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-main>

    <el-footer>
      <div class="block">
        <el-pagination
          :current-page="page.current"
          :page-sizes="[5,10, 20, 50, 100]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-footer>

    <el-dialog
      title="删除确认"
      :visible.sync="dialogConfirmVisible"
      :close-on-click-modal="false"
      width="30%"
      append-to-body
      center
    >
      <span>确认删除该条记录吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogConfirmVisible = false">取 消</el-button>
        <el-button type="danger" @click="deleteTask">确 定</el-button>
      </span>
    </el-dialog>

  </el-container>
</template>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>

<script>
import request from '@/utils/request'

export default {
  data: function() {
    return {
      conditions: [
        {
          field: 'name',
          type: 'STRING',
          value: '',
          symbol: 'LIKE'
        }
      ],
      baseUrl: process.env.VUE_APP_BASE_API,
      orders: [
        { field: 'create_time', rule: 'desc' }
      ],
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      jsonDictionaryList: [],
      currentJsonDictionaryTemp: {},
      dialogSourcesVisible: false,
      dialogConfirmVisible: false
    }
  },
  computed: {
    key_logic: function() {
      const { logic = {}} = this.$store.state.directory.directoryItem
      return logic
    }
  },
  created() {
    this.directoryUpdate()
    this.query()
  },
  methods: {
    directoryUpdate: function() {
      this.$store.commit('directory/setDirectory', 'logic')
    },
    editClick: function(row) {
      this.$emit('toChangePage', 'edit', row)
    },
    // 添加徽章点击事件
    addClick: function() {
      this.$emit('toChangePage', 'add')
    },
    deleteClick: function(row) {
      this.currentJsonDictionaryTemp = row
      this.dialogConfirmVisible = true
    },
    deleteTask: function() {
      this.$progress.start()
      request({
        url: '/preset/json_dictionary/delete',
        method: 'post',
        data: this.currentJsonDictionaryTemp
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    // 搜索
    search: function() {
      this.page.current = 1
      this.query()
    },
    query: function() {
      this.$progress.start()
      this.page.conditions = this.conditions
      this.page.orders = this.orders
      request({
        url: '/preset/json_dictionary/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.jsonDictionaryList = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
